/*
 * Copyright (c) 2024. Shanghai HEADING information Engineering Co., Ltd. All rights reserved.
 */
package com.lovely602.design.study.reflex;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author lizhixing
 */
public class ReflexClassTest {

  public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchFieldException {

    // 通过全类名反射
    Class<?> clazz = Class.forName("com.lovely602.design.study.reflex.ReflexClass");
    Object obj = clazz.newInstance();

    Method setCodeMethod = clazz.getMethod("setCode", String.class);
    setCodeMethod.invoke(obj, "123456");

    Method getCodeMethod = clazz.getMethod("getCode");
    Object code = getCodeMethod.invoke(obj);

    Field codeField = clazz.getField("code");
    String fieldName = codeField.getName();
    Object fieldValue = codeField.get(obj);

    System.out.println("code: " + fieldValue);
    System.out.println("class: " + obj.getClass().getName());

  }

}
